<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of findnearest</title>
  <meta name="keywords" content="findnearest">
  <meta name="description" content="FINDNEAREST finds from a array of points the nearest to a certain point.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
  <script type="text/javascript">
    if (top.frames.length == 0) { top.location = "../../index.html"; };
  </script>
</head>
<body>
<a name="_top"></a>
<!-- # RADOCCToolbox --><!-- menu.html CornerFinder -->
<h1>findnearest
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>FINDNEAREST finds from a array of points the nearest to a certain point.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function [nxpts,ixs]=findnearest(p,pts,num,same) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> FINDNEAREST finds from a array of points the nearest to a certain point.
 
 FINDNEAREST finds the nearest num points to point p from points pts. If
 same is input, the same point will be returned if it exists in pts.
 
 USAGE:
     [nxpts,ixs]=findnearest(p,pts,num); gets the nearest num pts
 
     [nxpts,ixs]=findnearest(p,pts,num,1); the same point is returned if
     it exists
 
 INPUTS:
     pt: reference point
 
     pts: 2xN array of points
 
     num: number of nearest points required
 
     same: flag, if input allows the function to return point pt
 
 OUTPUTS:
     nxpts: 2xnum array containing the found points
 
     ixs: the column coordinates of the points</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="getgrid.html" class="code" title="function crnrsgridout=getgrid(crnrs,pixs,peaklocs,ix,iy,debug)">getgrid</a>	GETGRID arranges the points that pass the chessboard filter into a grid.</li><li><a href="getwin.html" class="code" title="function win=getwin(img,pt,crnrpts)">getwin</a>	GETWIN chooses an appropriate window size for the chessboard corner filter</li></ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [nxpts,ixs]=findnearest(p,pts,num,same)</a>
0002 <span class="comment">% FINDNEAREST finds from a array of points the nearest to a certain point.</span>
0003 <span class="comment">%</span>
0004 <span class="comment">% FINDNEAREST finds the nearest num points to point p from points pts. If</span>
0005 <span class="comment">% same is input, the same point will be returned if it exists in pts.</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% USAGE:</span>
0008 <span class="comment">%     [nxpts,ixs]=findnearest(p,pts,num); gets the nearest num pts</span>
0009 <span class="comment">%</span>
0010 <span class="comment">%     [nxpts,ixs]=findnearest(p,pts,num,1); the same point is returned if</span>
0011 <span class="comment">%     it exists</span>
0012 <span class="comment">%</span>
0013 <span class="comment">% INPUTS:</span>
0014 <span class="comment">%     pt: reference point</span>
0015 <span class="comment">%</span>
0016 <span class="comment">%     pts: 2xN array of points</span>
0017 <span class="comment">%</span>
0018 <span class="comment">%     num: number of nearest points required</span>
0019 <span class="comment">%</span>
0020 <span class="comment">%     same: flag, if input allows the function to return point pt</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% OUTPUTS:</span>
0023 <span class="comment">%     nxpts: 2xnum array containing the found points</span>
0024 <span class="comment">%</span>
0025 <span class="comment">%     ixs: the column coordinates of the points</span>
0026 
0027 <span class="keyword">if</span> size(pts,1)&gt;2
0028     pts=pts';
0029 <span class="keyword">end</span>
0030 dist=sqrt((p(1)-pts(1,:)).^2+(p(2)-pts(2,:)).^2);
0031 <span class="comment">% if nargin&lt;4</span>
0032 <span class="comment">%     ind=find(dist==0,1);</span>
0033 <span class="comment">%     if ~isempty(ind)</span>
0034 <span class="comment">%         pts(:,ind)=[NaN;NaN];</span>
0035 <span class="comment">%         dist(ind)=NaN;</span>
0036 <span class="comment">%     end</span>
0037 <span class="comment">% end</span>
0038 
0039 [dist,ix]=sort(dist);
0040 
0041 <span class="keyword">if</span> nargin&lt;4
0042     ind=find(dist==0,1);
0043     ix(ind)=[];
0044 <span class="keyword">end</span>
0045 nxpts=pts(:,ix(1:num));
0046 ixs=ix(1:num);</pre></div>
<hr><address>Generated on Sun 04-Apr-2010 17:13:59 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
</body>
</html>